home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 4 / Macwelt DVD 4.cdr / Entwickler / Mac-OS / oxygen / samples / docbook / xsl / html / html-rtf.xsl < prev    next >
Encoding:
Extensible Markup Language  |  2002-09-09  |  6.8 KB  |  189 lines  |  [□□□□/□□□□]

  1. html-rtf.xsl°π¢tÚπ¢tÚÅÅñ∑<?xml version="1.0"?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3.                 xmlns:exsl="http://exslt.org/common"
  4.                 xmlns:set="http://exslt.org/sets"
  5.                 exclude-result-prefixes="exsl set"
  6.                 version="1.0">
  7.  
  8. <!-- This module contains templates that match against HTML nodes. It is used
  9.      to post-process result tree fragments for some sorts of cleanup.
  10.      These templates can only ever be fired by a processor that supports
  11.      exslt:node-set(). -->
  12.  
  13. <!-- ==================================================================== -->
  14.  
  15. <!-- insert.html.p mode templates insert a particular RTF at the beginning
  16.      of the first paragraph in the primary RTF. -->
  17.  
  18. <xsl:template match="/" mode="insert.html.p">
  19.   <xsl:param name="mark" select="'?'"/>
  20.   <xsl:apply-templates mode="insert.html.p">
  21.     <xsl:with-param name="mark" select="$mark"/>
  22.   </xsl:apply-templates>
  23. </xsl:template>
  24.  
  25. <xsl:template match="*" mode="insert.html.p">
  26.   <xsl:param name="mark" select="'?'"/>
  27.   <xsl:copy>
  28.     <xsl:copy-of select="@*"/>
  29.     <xsl:apply-templates mode="insert.html.p">
  30.       <xsl:with-param name="mark" select="$mark"/>
  31.     </xsl:apply-templates>
  32.   </xsl:copy>
  33. </xsl:template>
  34.  
  35. <xsl:template match="p" mode="insert.html.p">
  36.   <xsl:param name="mark" select="'?'"/>
  37.   <xsl:copy>
  38.     <xsl:copy-of select="@*"/>
  39.     <xsl:if test="not(preceding::p)">
  40.       <xsl:copy-of select="$mark"/>
  41.     </xsl:if>
  42.     <xsl:apply-templates mode="insert.html.p">
  43.       <xsl:with-param name="mark" select="$mark"/>
  44.     </xsl:apply-templates>
  45.   </xsl:copy>
  46. </xsl:template>
  47.  
  48. <xsl:template match="text()|processing-instruction()|comment()" mode="insert.html.p">
  49.   <xsl:param name="mark" select="'?'"/>
  50.   <xsl:copy/>
  51. </xsl:template>
  52.  
  53. <!-- ==================================================================== -->
  54.  
  55. <!-- insert.html.text mode templates insert a particular RTF at the beginning
  56.      of the first text-node in the primary RTF. -->
  57.  
  58. <xsl:template match="/" mode="insert.html.text">
  59.   <xsl:param name="mark" select="'?'"/>
  60.   <xsl:apply-templates mode="insert.html.text">
  61.     <xsl:with-param name="mark" select="$mark"/>
  62.   </xsl:apply-templates>
  63. </xsl:template>
  64.  
  65. <xsl:template match="*" mode="insert.html.text">
  66.   <xsl:param name="mark" select="'?'"/>
  67.   <xsl:copy>
  68.     <xsl:copy-of select="@*"/>
  69.     <xsl:apply-templates mode="insert.html.text">
  70.       <xsl:with-param name="mark" select="$mark"/>
  71.     </xsl:apply-templates>
  72.   </xsl:copy>
  73. </xsl:template>
  74.  
  75. <xsl:template match="text()|processing-instruction()|comment()" mode="insert.html.text">
  76.   <xsl:param name="mark" select="'?'"/>
  77.  
  78.   <xsl:if test="not(preceding::text())">
  79.     <xsl:copy-of select="$mark"/>
  80.   </xsl:if>
  81.  
  82.   <xsl:copy/>
  83. </xsl:template>
  84.  
  85. <xsl:template match="processing-instruction()|comment()" mode="insert.html.text">
  86.   <xsl:param name="mark" select="'?'"/>
  87.   <xsl:copy/>
  88. </xsl:template>
  89.  
  90. <!-- ==================================================================== -->
  91.  
  92. <!-- unwrap.p mode templates remove blocks from HTML p elements (and
  93.      other places where blocks aren't allowed) -->
  94.  
  95. <xsl:template name="unwrap.p">
  96.   <xsl:param name="p"/>
  97.   <xsl:choose>
  98.     <xsl:when test="function-available('exsl:node-set')
  99.                     and function-available('set:leading')
  100.                     and function-available('set:trailing')">
  101.       <xsl:apply-templates select="exsl:node-set($p)" mode="unwrap.p"/>
  102.     </xsl:when>
  103.     <xsl:otherwise>
  104.       <xsl:copy-of select="$p"/>
  105.     </xsl:otherwise>
  106.   </xsl:choose>
  107. </xsl:template>
  108.  
  109. <xsl:template match="p" mode="unwrap.p">
  110.   <xsl:variable name="blocks" select="div|p|blockquote|table"/>
  111.  
  112.   <xsl:choose>
  113.     <xsl:when test="$blocks">
  114.       <xsl:call-template name="unwrap.nodes">
  115.         <xsl:with-param name="wrap" select="."/>
  116.         <xsl:with-param name="first" select="1"/>
  117.         <xsl:with-param name="nodes" select="node()"/>
  118.         <xsl:with-param name="blocks" select="$blocks"/>
  119.       </xsl:call-template>
  120.     </xsl:when>
  121.     <xsl:otherwise>
  122.       <xsl:copy>
  123.         <xsl:copy-of select="@*"/>
  124.         <xsl:apply-templates mode="unwrap.p"/>
  125.       </xsl:copy>
  126.     </xsl:otherwise>
  127.   </xsl:choose>
  128. </xsl:template>
  129.  
  130. <xsl:template match="*" mode="unwrap.p">
  131.   <xsl:copy>
  132.     <xsl:copy-of select="@*"/>
  133.     <xsl:apply-templates mode="unwrap.p"/>
  134.   </xsl:copy>
  135. </xsl:template>
  136.  
  137. <xsl:template match="text()|processing-instruction()|comment()" mode="unwrap.p">
  138.   <xsl:copy/>
  139. </xsl:template>
  140.  
  141. <xsl:template name="unwrap.nodes">
  142.   <xsl:param name="wrap" select="."/>
  143.   <xsl:param name="first" select="0"/>
  144.   <xsl:param name="nodes"/>
  145.   <xsl:param name="blocks"/>
  146.   <xsl:variable name="block" select="$blocks[1]"/>
  147.  
  148.   <xsl:choose>
  149.     <xsl:when test="$blocks">
  150.       <xsl:variable name="leading" select="set:leading($nodes,$blocks)"/>
  151.       <xsl:variable name="trailing" select="set:trailing($nodes,$blocks)"/>
  152.  
  153.       <xsl:element name="{local-name($wrap)}" namespace="{namespace-uri($wrap)}">
  154.         <xsl:for-each select="$wrap/@*">
  155.           <xsl:if test="$first != 0 or local-name(.) != 'id'">
  156.             <xsl:copy/>
  157.           </xsl:if>
  158.         </xsl:for-each>
  159.         <xsl:apply-templates select="$leading" mode="unwrap.p"/>
  160.       </xsl:element>
  161.  
  162.       <xsl:apply-templates select="$block" mode="unwrap.p"/>
  163.  
  164.       <xsl:if test="$trailing">
  165.         <xsl:call-template name="unwrap.nodes">
  166.           <xsl:with-param name="wrap" select="$wrap"/>
  167.           <xsl:with-param name="nodes" select="$trailing"/>
  168.           <xsl:with-param name="blocks" select="$blocks[position() > 1]"/>
  169.         </xsl:call-template>
  170.       </xsl:if>
  171.     </xsl:when>
  172.  
  173.     <xsl:otherwise>
  174.       <xsl:element name="{local-name($wrap)}" namespace="{namespace-uri($wrap)}">
  175.         <xsl:for-each select="$wrap/@*">
  176.           <xsl:if test="$first != 0 or local-name(.) != 'id'">
  177.             <xsl:copy/>
  178.           </xsl:if>
  179.         </xsl:for-each>
  180.         <xsl:apply-templates select="$nodes" mode="unwrap.p"/>
  181.       </xsl:element>
  182.     </xsl:otherwise>
  183.   </xsl:choose>
  184. </xsl:template>
  185.  
  186. <!-- ==================================================================== -->
  187.  
  188. </xsl:stylesheet>
  189. This resource fork intentionally left blank   ˇˇ